import time
from tokenize import String

import pytest
from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement

from action.kdt import KeyWord
from pages.LoginPage import LoginPage

global_var = {}
driver = webdriver.Chrome()


@pytest.fixture(scope="session")
def login():
    login_page = LoginPage(driver)
    login_page.goto_url("https://www.moguoyunke.com")
    driver.maximize_window()
    login_page.login("15732069727", "Zhixin@2025")
    msg = login_page.msg("xpath", '//p[@class="el-message__content"]')
    assert msg == "登录成功"
    time.sleep(2)
    yield driver
    driver.quit()


def pytest_xlsx_run_step(item):  # yaml用例的每一个步骤（step）都会调用一次本hook
    print('用例名称', item.name)
    print('当前步骤', item.current_step)
    kd = KeyWord(item.usefixtures['login'])
    key = item.current_step['标记']
    func = getattr(kd, key)
    args_list = []
    my_classify_list = []
    for item_temp_step_key, item_temp_step_value in item.current_step.items():
        if '列' in item_temp_step_key and item_temp_step_value != None:
            args_list.append(item_temp_step_value)
            print("过滤后的参数列表", args_list)

    for arg in args_list:
        if ">" in arg:
            arg_have_var=arg
            args_list.remove(arg)
            if "$" not in arg:
                var_name = arg_have_var.split(">")[1]
                time.sleep(2)
                global_var[var_name] = func(*args_list)
                var_value = global_var[var_name]
                if isinstance(var_value, (list, tuple)):
                    for data in global_var[var_name]:
                        my_classify_list.append(data.text)

                    print("原有的我的分类列表显示", my_classify_list)
                    print("全局变量的展示",global_var)

            else:
                index = args_list.index(arg)
                var_name=arg.split("${")[1].split("}")[0]

                var_value = global_var[var_name]

                if isinstance(var_value, list):
                    if isinstance(var_value[0], WebElement):
                        args_list[index] = arg.replace("${" + var_name + "}", var_value[0].text)
                        func(*args_list)
                        global_var[var_name].remove(var_value[0])
                else:
                    args_list[index] = arg.replace(arg, var_value)
                    func(*args_list)
                    global_var[var_name].remove(var_value)

        # print("全局变量的展示", global_var)
        else:
            if "$" in arg:
                index = args_list.index(arg)
                var_name = arg.split("${")[1].split("}")[0]

                var_value = global_var[var_name]

                if isinstance(var_value, list):
                    if isinstance(var_value[0], WebElement):
                        args_list[index] = arg.replace("${"+var_name+"}", var_value[0].text)
                    else:
                        args_list[index] = arg.replace(arg, var_value)
                else:
                    args_list[index] = arg.replace(arg, var_value)
                print("变量替换值",args_list)
                time.sleep(2)
                func(*args_list)
                print("调用成功")
                time.sleep(2)
    else:
        print("没有参数需要处理")
        time.sleep(2)
        func(*args_list)

    print("处理后的列表显示", args_list)
    return True
